using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace MessageLibrary
{
    [Serializable]
    public class ContentMessage : AbsMessage
    {
        string rtf = string.Empty;

        /// <summary>
        /// A content message that contains
        /// text, color, fonts and emoticons
        /// </summary>
        /// <param name="name">sender user name</param>
        /// <param name="rtf">message content in rtf format</param>
        public ContentMessage(string name, string rtf): base (name)
        {
            this.rtf = rtf;
        }



        public override void HandleMessage(RichTextBox textBox, Client c)
        {
            //checks if there is a text box to update.
            //also checks if a client object was entered,
            //if it was then the message is handled in
            //the server side and does not need to update 
            //ant textbox.
            if (textBox == null || c!=null) return ;

            //checks if invoke is required
            if (textBox.InvokeRequired)
            {
                MessageInvoker mi = new MessageInvoker(this.HandleMessage);
                textBox.Invoke(mi, textBox, c);
            }
            else
            {
                try
                {
                    //finds the end of the chat conversation
                    textBox.SelectionStart = textBox.Text.Length;
                    //prints the time of the message
                    textBox.SelectedText = time.ToShortTimeString();
                    //print the sender of the message
                    textBox.SelectedText = " " + username + ": ";
                    //prints the given message
                    textBox.SelectedRtf = rtf;
                    //scrolls the text box down
                    textBox.ScrollToCaret();                    
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, e.Source);
                }
            }
        }
    }
}
